package xiaopiya.robot.handler.translate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

/**
 * @author Ray (ayufox@gmail.com)
 * @version 0.1
 */
public class GoogleApiTranslateService implements ITranslateService
{
	private final static Log LOG = LogFactory.getLog(GoogleApiTranslateService.class);
	
	/* (non-Javadoc)
	 * @see xiaopiya.robot.handler.translate.ITranslateService#isValidLanguage(java.lang.String)
	 */
	public boolean isValidLanguage(String lang)
	{
		return Language.isValidLanguage(lang);
	}
	
	/* (non-Javadoc)
	 * @see xiaopiya.robot.handler.translate.ITranslateService#translate(java.lang.String, java.lang.String, java.lang.String)
	 */
	public String translate(String from, String to, String source)
	{
		if (LOG.isInfoEnabled())
		{
			LOG.info("translat[" + source + "] from [" + from + "] to [" + to + "]");
		}
		if (!Language.isValidLanguage(from))
		{
			return from + "语言不存在";
		}
		else if (!Language.isValidLanguage(to))
		{
			return to + "语言不存在";
		}
		
		try
		{
			return Translate.translate(source, from, to);
		} catch (Exception e)
		{
			LOG.error("error when translat", e);
			return "连接翻译服务器错误";
		}
	}

}
